package com.shop.server.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * @author ghy
 * @version 1.0
 * @date 2020-10-30
 */
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/**").permitAll()
                .anyRequest().authenticated()
                .and().csrf().disable();
    }

    @Bean
    public BCryptPasswordEncoder bcryptPasswordEncoder(){
        return new BCryptPasswordEncoder();
    }


    public static void main(String[] args) {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();

        //加密
        String pwd = encoder.encode("saopfjasngflasjgasdklgdas23sad1f3sadf");
        System.out.println(pwd);   //随机盐   时间戳
        //$2a$10$XPDPNla1e/ALMvFDbryquODA.A7JLDkr/T7BaReBJbmUHdefaigvW
        //$2a$10$G61li2yZBqgRJxfN.Stf5OJ1qhdmWvty2iN8KIRRglLx8LlN6p5Le
        System.out.println(pwd.length());
        //60

        //校验密码
        boolean f1 = encoder.matches("123456", "$2a$10$G61li2yZBqgRJxfN.Stf5OJ1qhdmWvty2iN8KIRRglLx8LlN6p5Le");
        boolean f2 = encoder.matches("123456", "$2a$10$XPDPNla1e/ALMvFDbryquODA.A7JLDkr/T7BaReBJbmUHdefaigvW");

        System.out.println(f1);
        System.out.println(f2);


    }
}
